package cn.edu.cqvie.ssm.model.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import cn.edu.cqvie.ssm.entity.User;
import cn.edu.cqvie.ssm.model.UserModel;
import cn.edu.cqvie.ssm.util.Dbconn;

/**
 * 用户模型
 * 
 * @author zhengsh
 *
 */
public class UserModelImpl implements UserModel {

	Dbconn db = Dbconn.builder().build();

	/**
	 * 查询用户列表
	 * 
	 * @return
	 */
	public List<User> selectUser() {
		return selectUser(null);
	}

	/**
	 * 通过ID查询用户信息
	 * 
	 * @param id 用户ID
	 * @return
	 */
	public User load(Integer id) {
		User user = null;
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "select * from user where id =?";
		try {
			con = db.getConnection();
			ps = (PreparedStatement) con.prepareStatement(sql);
			ps.setInt(1, id);
			rs = ps.executeQuery();
			while (rs.next()) {
				user = User.builder().id(rs.getInt("id")).name(rs.getString("name")).password(rs.getString("password"))
						.build();
			}
		} catch (Throwable e) {
			e.printStackTrace();
		}
		return user;
	}

	@Override
	public List<User> selectUser(User user) {
		List<User> list = new ArrayList<>();
		Connection con = null;
		Statement st = null;
		ResultSet rs = null;
		try {
			con = db.getConnection();
			st = con.createStatement();
			String sql = "select * from user";
			rs = st.executeQuery(sql);
			while (rs.next()) {
				User u = User.builder().id(rs.getInt("id")).name(rs.getString("name"))
						.password(rs.getString("password")).build();
				list.add(u);
			}
		} catch (Throwable e) {
			e.printStackTrace();
		} finally {
			db.closeAll(con, st, rs);
		}
		return list;
	}

	@Override
	public void save(User user) {
		// TODO Auto-generated method stub
		Connection conn = null;
		PreparedStatement ps = null;
		String sql = "insert into user(name,password) values(?,?)";
		try {
			conn = db.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setString(1, user.getName());
			ps.setString(2, user.getPassword());
			ps.executeUpdate();

		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("保存失败");
		}

	}

	@Override
	public void delete(Integer id) {
		Connection con = null;
		PreparedStatement ps = null;
		String sql = "delete from user where id=?";
		try {
			con = db.getConnection();
			ps = con.prepareStatement(sql);
			ps.setInt(1, id);
			ps.executeUpdate();
			
			ps.close();
			con.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void update(User user) {
		Connection con = null;
		PreparedStatement ps = null;
		String sql = "update user set name=?, password=? where id=?";
		try {
			con = db.getConnection();
			ps = con.prepareStatement(sql);
			ps.setString(1, user.getName());
			ps.setString(2, user.getPassword());
			ps.setInt(3, user.getId());
			ps.executeUpdate();
			
			ps.close();
			con.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public boolean find(String user,String password) {
		boolean reset = false;
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "select name,password from user where name=? and password=?";
		try {
			con = db.getConnection();
			ps = con.prepareStatement(sql);
			ps.setString(1, user);
			ps.setString(2, password);
			rs = ps.executeQuery();
			while(rs.next()) {
				reset = true;
			}
			rs.close();
			ps.close();
			con.close();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return reset;
	}
}
